package br.fatec.model.entity;

import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;

@Entity
public class Cliente {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(insertable = false, updatable = false)
	private Integer codigo;

	@Column(length = 200, nullable = false)
	private String nome;

	@Column(length = 20, nullable = true)
	private String cpf;

	@Column(length = 200, nullable = true)
	private String email;

	@OneToOne(cascade = CascadeType.REMOVE, optional=false)
	@JoinColumn(foreignKey=@ForeignKey(name = "fk_cliente_login"))
	private Usuario login;

	@Column(length = 2000, nullable = true)
	private String observacoes;

	@ManyToMany(fetch = FetchType.EAGER, cascade=CascadeType.REMOVE)
	@JoinColumn(foreignKey=@ForeignKey(name = "fk_contato_cod", foreignKeyDefinition="fk_cliente_cod"))
	@JoinTable(name = "cliente_contato", joinColumns = @JoinColumn(name = "cliente_cod", nullable = false, foreignKey=@ForeignKey(name = "fk_cliente")), inverseJoinColumns = @JoinColumn(name = "contato_cod", nullable = false, foreignKey=@ForeignKey(name = "fk_contato")))
	@OrderBy("codigo asc")
	private Set<Contato> contatos = new HashSet<Contato>();

	@ManyToOne
	@JoinColumn(nullable = false, updatable = false, foreignKey=@ForeignKey(name = "fk_cliente_criadopor"))
	private Usuario criadoPor;

	@GeneratedValue
	@Column(nullable = false, columnDefinition = "timestamp default now()", insertable = false, updatable = false)
	private Timestamp criadoEm;

	@Column(nullable = false, columnDefinition = "boolean default true", insertable = false)
	private Boolean ativo;
	
	@Column(nullable = false, columnDefinition = "boolean default false", insertable = false)
	private Boolean integrado;

	public Integer getCodigo() {
		return codigo;
	}

	public void setCodigo(Integer codigo) {
		this.codigo = codigo;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		if (nome != null)
			this.nome = nome.trim();
	}

	public String getCpf() {
		return cpf;
	}

	public void setCpf(String cpf) {
		this.cpf = cpf;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		if (email != null)
			this.email = email.trim();
	}

	public Usuario getLogin() {
		return login;
	}

	public void setLogin(Usuario login) {
		this.login = login;
	}

	public String getObservacoes() {
		return observacoes;
	}

	public void setObservacoes(String observacoes) {
		if (observacoes != null)
			this.observacoes = observacoes.trim();
	}

	public Set<Contato> getContatos() {
		return contatos;
	}

	public void setContatos(Set<Contato> contatos) {
		this.contatos = contatos;
	}

	public Usuario getCriadoPor() {
		return criadoPor;
	}

	public void setCriadoPor(Usuario criadoPor) {
		this.criadoPor = criadoPor;
	}

	public Timestamp getCriadoEm() {
		return criadoEm;
	}

	public void setCriadoEm(Timestamp criadoEm) {
		this.criadoEm = criadoEm;
	}

	public Boolean getAtivo() {
		return ativo;
	}

	public void setAtivo(Boolean ativo) {
		this.ativo = ativo;
	}

	public Boolean getIntegrado() {
		return integrado;
	}

	public void setIntegrado(Boolean integrado) {
		this.integrado = integrado;
	}
}
